package com.topone.service.impl;

import com.topone.Const.FanzoneServiceConst;
import com.topone.Exception.FanZoneException;
import com.topone.Utils.CreateQReCodeUtils;
import com.topone.dataObject.Bussiness;
import com.topone.dataObject.Image;
import com.topone.service.BussinessService;
import com.topone.service.ImageService;
import com.topone.service.PictureService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.OutputStream;
import java.util.Objects;

@Service
public class PictureServiceImpl implements PictureService {

    @Autowired
    private BussinessService bussinessService;
    @Autowired
    private ImageService imageService;

    @Override
    public byte[] picturePay(String businessId, HttpServletResponse response) {
        Bussiness bussiness = bussinessService.getOne(businessId);
        if (Objects.isNull(bussiness)) {
            throw new FanZoneException(404, "商家不存在");
        }
        String url = bussiness.getBussinessIcon();
        String imageIdText = StringUtils.substringAfterLast(url, FanzoneServiceConst.subSpilt);
        Integer imageId = Integer.valueOf(imageIdText);
        Image image1 = imageService.findImageById(imageId);
        File file1 = new File(image1.getImgPath());
        if(!file1.exists()){
            file1=new File(FanzoneServiceConst.defaultLogoImage);
        }
            File file = new File(FanzoneServiceConst.backGroundImage);
        try {
            BufferedImage logoImage = ImageIO.read(file1);
            BufferedImage backgroundImage = ImageIO.read(file);
            BufferedImage image = CreateQReCodeUtils.createQRCode(FanzoneServiceConst.payPicture + businessId, logoImage, backgroundImage);
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            ImageIO.write(image, "png", outputStream);
            response.setContentType("application/x-png");
            response.setHeader("Content-Disposition","attachment;filename=支付_"+bussiness.getBussinessId()+".png");
            OutputStream fos = response.getOutputStream();
            fos.write(outputStream.toByteArray());
            fos.flush();
            fos.close();
            outputStream.close();
            return outputStream.toByteArray();
        } catch (Exception e) {
            throw new FanZoneException(500, "下载图片错误");
        }
    }

    @Override
    public byte[] pictureInfo(String businessId,  HttpServletResponse response) {
        Bussiness bussiness = bussinessService.getOne(businessId);
        if (Objects.isNull(bussiness)) {
            throw new FanZoneException(404, "商家不存在");
        }
        String url = bussiness.getBussinessIcon();
        String imageIdText = StringUtils.substringAfterLast(url, FanzoneServiceConst.subSpilt);
        Integer imageId = Integer.valueOf(imageIdText);
        Image image1 = imageService.findImageById(imageId);
        File file1 = new File(image1.getImgPath());
        if(!file1.exists() || file1==null){
            file1=new File(FanzoneServiceConst.defaultLogoImage);
        }
        File file = new File(FanzoneServiceConst.backGroupImageInfo);
        try {
            BufferedImage logoImage = ImageIO.read(file1);
            BufferedImage backgroundImage = ImageIO.read(file);
            BufferedImage image = CreateQReCodeUtils.createQRCode(FanzoneServiceConst.infoPicture + businessId, logoImage, backgroundImage);
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            ImageIO.write(image, "png", outputStream);
            byte[] bytes = outputStream.toByteArray();
            response.setContentType("application/x-png");
            response.setHeader("Content-Disposition","attachment;filename=领券_"+bussiness.getBussinessId()+".png");
            OutputStream fos = response.getOutputStream();
            fos.write(bytes);
            fos.flush();
            fos.close();
            outputStream.close();
            return bytes;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return new byte[0];
    }
}
